<template>
  <div
    :class="className"
    :style="{height: height, width: width}"
  />
</template>

<script lang="ts">
import * as echarts from 'echarts'
import { EChartsOption } from 'echarts'
import { Component, Prop } from 'vue-property-decorator'
import { mixins } from 'vue-class-component'
import ResizeMixin from '@/components/Charts/mixins/resize'
import { getBranchIssuesTickets } from '@/api-scp/dashboard'
import { BranchIssuesTickets } from '@/api-scp/types'

@Component({
  name: 'BranchesScoresChart',
  components: {}
})
export default class extends mixins(ResizeMixin) {
  @Prop({ default: 'chart' }) private className!: string
  @Prop({ default: '100%' }) private width!: string
  @Prop({ default: '500px' }) private height!: string

  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  }

  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  }

  private async initChart() {
    this.chart = echarts.init(this.$el as HTMLDivElement)

    const data = await getBranchIssuesTickets()
    this.setOptions(data)
  }

  private setOptions(data: BranchIssuesTickets[]) {
    if (!this.chart) {
      return
    }

    const series = data.map(item => {
      return {
        name: item.name,
        type: 'scatter',
        data: [
          [item.regs, item.tickets]
        ]
      } as any
    })

    const option: EChartsOption = {
      legend: {
        type: 'scroll',
        orient: 'vertical',
        right: 0,
        top: 20,
        bottom: 20
      },
      grid: { right: '140' },
      tooltip: {
        trigger: 'item',
        axisPointer: {
          type: 'cross'
        }
      },
      symbolSize: 20,
      emphasis: {
        focus: 'series'
      },
      xAxis: { name: '合规管理结果得分' },
      yAxis: { name: '合规管理基础得分' },
      series
    }

    console.log(JSON.stringify(option, null, 2))

    this.chart.setOption(option)
  }
}
</script>

<style lang="scss" scoped>

</style>
